﻿
var componentManager = function () {
    this.components = {};
    this.register = function (component) {
        var guid = Utils.getGuid();
        this.components[guid] = component;
        return guid;
    };
    this.get = function (name) {
        if (name == null || name == "") return false;
        for (var component in this.components) {
            if (component == name) {
                return this.components[component];
            }
        }
        return false;
    };
    this.remove = function (name) {
        if (this.components[name]) {
            delete this.components[name];
        }
    };
    this.exists = function (name) {
        if (name == null) return false;
        for (var layout in this.layouts) {
            if (layout == name) {
                return true;
            }
        }
        return false;
    };
    this.getIdByEvent = function (event) {
        var jo = $(event.target);
        var o = jo.closest("componentid");
        if (o.size() > 0) {
            return o.attr("componentid");
        }
        return mWindowComponent.componentid;
    };
    this.loadUrlto = function (url, jo, fromcomponentid, callback) {
        var iframe = document.createElement("iframe");
        iframe.style = "width:100%;height:100%";
        iframe.width = "100%";
        iframe.height = "100%";
        iframe.frameborder = 0;
        iframe.src = url;
        iframe.fromcomponentid = fromcomponentid;
        iframe.callback = callback;
        if (iframe.attachEvent) {
            iframe.attachEvent("onload", function () {
                if (this.callback) this.callback();
                this.contentWindow.mWindowComponent.setFromComponent(this.fromcomponentid);
            });
        } else {
            iframe.onload = function () {
                if (this.callback) this.callback();
                this.contentWindow.mWindowComponent.setFromComponent(this.fromcomponentid);
            };
        }
        jo.append($(iframe));
    }
};
var mComponentManager = new componentManager();
if (top != self) {
    mComponentManager = top.mComponentManager;
}

var windowComponent = function () {
    this.fromComponent = null;
    this.componentid = null;
    this.changeHandler = function () {
        location.reload(false);
    };
    this.isChanged = false;
    this.init = function () {
        //var fromcompid = Utils.getArgs("fromComponent");
        //this.fromComponent = top.mComponentManager.get(fromcompid);
    };
    this.setFromComponent = function (fromcompid) {
        this.fromComponent = top.mComponentManager.get(fromcompid);

    };
    this.change = function () {
        if (this.changeHandler) {
            this.changeHandler();
            return;
        }
        if (window["_hyt_changehandler"]) {
            window["_hyt_changehandler"]();
            return;
        }
    };
    this.focus = function () {
        if (this.isChanged) {
            this.change();
            this.isChanged = false;
        }
    };
    this.notifyParent = function () {
        if (this.fromComponent) {
            this.fromComponent.isChanged = true;
        }
    };
};

var mWindowComponent = new windowComponent();
//mWindowComponent.init();
mWindowComponent.componentid = mComponentManager.register(mWindowComponent);

